home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_21375.txt < prev    next >
Text File  |  1990-04-17  |  11KB  |  415 lines

  1. -- card: 21375 from stack: in.3
  2. -- bmap block id: 22059
  3. -- flags: 4000
  4. -- background id: 7836
  5. -- name: PopList
  6. ----- HyperTalk script -----
  7. on opencard
  8.   global nextPos
  9.   hide card field "source"
  10.   set the scroll of card field "documentation" to 0
  11.   put 0 into nextPos
  12.   pass opencard
  13. end opencard
  14.  
  15. on Install
  16.   get ChooseTargetStack()
  17.   InstallResource XFCN,PopList,it
  18. end Install
  19.  
  20.  
  21. -- part 5 (field)
  22. -- low flags: 01
  23. -- high flags: 2007
  24. -- rect: left=18 top=32 right=288 bottom=480
  25. -- title width / last selected line: 0
  26. -- icon id / first selected line: 0 / 0
  27. -- text alignment: 0
  28. -- font id: 3
  29. -- text size: 10
  30. -- style flags: 0
  31. -- line height: 13
  32. -- part name: Documentation
  33.  
  34.  
  35. -- part 8 (field)
  36. -- low flags: 81
  37. -- high flags: 0007
  38. -- rect: left=18 top=31 right=289 bottom=492
  39. -- title width / last selected line: 0
  40. -- icon id / first selected line: 0 / 0
  41. -- text alignment: 0
  42. -- font id: 3
  43. -- text size: 10
  44. -- style flags: 0
  45. -- line height: 13
  46. -- part name: source
  47.  
  48.  
  49. -- part 10 (button)
  50. -- low flags: 00
  51. -- high flags: 8003
  52. -- rect: left=304 top=299 right=321 bottom=425
  53. -- title width / last selected line: 0
  54. -- icon id / first selected line: 0 / 0
  55. -- text alignment: 1
  56. -- font id: 0
  57. -- text size: 12
  58. -- style flags: 0
  59. -- line height: 16
  60. -- part name: Show C Source
  61. ----- HyperTalk script -----
  62. on mouseUp
  63.   get the visible of card field "source"
  64.   set the visible of card field "source" to not it
  65.   if it is false then
  66.     set the name of me to "Hide C Source"
  67.   else
  68.     set the name of me to "Show C Source"
  69.   end if
  70. end mouseUp
  71.  
  72.  
  73.  
  74. -- part 11 (button)
  75. -- low flags: 00
  76. -- high flags: 8004
  77. -- rect: left=351 top=45 right=66 bottom=412
  78. -- title width / last selected line: 0
  79. -- icon id / first selected line: 0 / 0
  80. -- text alignment: 1
  81. -- font id: 0
  82. -- text size: 12
  83. -- style flags: 0
  84. -- line height: 16
  85. -- part name: PopList
  86. ----- HyperTalk script -----
  87. on mouseDown
  88.   global mylast
  89.   get the rect of me
  90.   put item 2 of it into myTop
  91.   put item 1 of it into myLeft
  92.   get PopList("\one,two,three,\(-,\(disabled item,12345", myLeft,myTop,myLast)
  93.   put it into myLast
  94.   put "PopList choice was: "&mylast
  95. end mouseDown
  96.  
  97.  
  98.  
  99. -- part contents for card part 5
  100. ----- text -----
  101. PopList 1.0d2
  102. Roger Brown
  103.  
  104. PopList is an XFCN that handles a pop-up menu.  Menu items may contain Menu
  105. Manager meta-characters.  Normally, those characters are not processed, so you
  106. can have menu items containing any text.  If you want the meta-characters processed
  107. for an item, put a back slash "\" in the first character position.  For example, in the  item list of "a,b,(c", the third item will be the literal string "(c".  In the list "a,b,\(c", the third item will be a disabled "c" because the "\" character tells the XFCN to process the "(" as a meta-character.
  108.  
  109.  
  110. Syntax is:  get PopList(menuItems,left,top,default)
  111.  
  112. where      menuItems is an item list of items. Ex. "Chicago,Helvetica,Times"
  113.                 left and top are the left,top coordinates of the pop-up rectangle in local card 
  114.                   coordinates
  115.                 default is an optional item number to indicate which item should be checked
  116.                   when the menu is first popped up
  117.  
  118.  
  119. returns the number of the item chosen or 0 if none was chosen.
  120.  
  121.  
  122.  
  123.  
  124. REVISION HISTORY
  125. 1.0d2  10/15/89 First public release.
  126.  
  127. -- part contents for card part 8
  128. ----- text -----
  129. /* PopList1.0d2.c */
  130. /* ┬⌐ Digital Medicine Inc. 1989 */
  131. /* written in THINK C  ┬⌐ Think Technologies, Inc */
  132. /* by Roger Brown 10/15/89  */
  133.  
  134. /* version 1.0d2: ignore blank items unless they start with \" */
  135. /* version 1.0d1: to process meta-characters, precede a menu item with a 
  136.    back slash "\" */
  137.    
  138. /* This is a HyperCard XFCN that handles popup menus.
  139.   
  140.    
  141.   HyperCard Syntax is:
  142.    
  143.         PopList items,left,top,default
  144.    
  145.      ex.  PopList "Chicago,Courier,Monaco,Times",100,100,1
  146.   
  147.     returns:    the number of the item chosen or 0 if none
  148.    
  149.     The menu is not remembered by the XCMD so the item list has to be passed 
  150.     each time.  Meta characters are not processed unless a menu item starts with 
  151.     the \ character. The left,top position is in coordinates local to the card
  152.     window.
  153.             
  154.     Parameters are menu item list, left and top coordinates of the 
  155.     default popup item, item number of the default popup item. 
  156.                                              
  157.     To compile: create a project with this, ANSI-A4 and MacTraps. Build as code 
  158.     resource type XFCN named PopList.
  159. */
  160.  
  161. #include "MenuMgr.h"
  162. #include "HyperXCmd.h"
  163. #include "XCmdGlue.inc.c"
  164. #include "SetUpA4.h"
  165.  
  166. #define FALSE 0
  167. #define TRUE !FALSE
  168. #define NULL 0L
  169.  
  170. char isNumber();
  171.  
  172. /* build a return result structure from a string */
  173.  
  174. ResultIs(paramPtr,theResult)
  175. XCmdBlockPtr    paramPtr;
  176. char *theResult;
  177. {
  178.     long len;
  179.     Handle resultHandle;
  180.     len = 1+strlen(theResult);
  181.     resultHandle = NewHandle(len);
  182.     BlockMove(theResult,*resultHandle,len);
  183.     paramPtr->returnValue = resultHandle;
  184. }
  185.  
  186. /* Get the number of HyperCard comma delimited items in string s. */
  187.  
  188. int NumHCItems(s)
  189. char *s;
  190. {
  191.     int c,len,count,j;
  192.     char temp[255];
  193.     long it;
  194.     
  195.     count = j = 0;
  196.     len = strlen(s);
  197.     for (c=0;c<len;c++) {
  198.         if (s[c]==',') {
  199.             count = count + 1;
  200.             j = 0;
  201.         }
  202.         else {
  203.             temp[j] = s[c];
  204.             j++;
  205.             if (c==(len-1)) { /* last item, no comma */
  206.                 count = count+1;
  207.                 break;
  208.             }
  209.         }
  210.     }
  211.     return count;
  212. }
  213.  
  214. /* Get HyperCard comma delimited item i from item list string inStr. 
  215.    Return it in outStr */
  216.  
  217. GetHCItem(inStr,i,outStr)
  218. char *inStr,*outStr;
  219. int i;
  220. {
  221.     int c,len,count,j;
  222.     char temp[255];
  223.     
  224.     count = j = 0;
  225.     len = strlen(inStr);
  226.     for (c=0;c<len;c++) {
  227.         if (inStr[c]==',') {
  228.             count = count + 1;
  229.             if (count==i) break;
  230.             j = 0;
  231.         }
  232.         else {
  233.             temp[j] = inStr[c];
  234.             j++;
  235.             if (c==(len-1)) { /* last item, no comma */
  236.                 count = count+1;
  237.                 break;
  238.             }
  239.         }
  240.     }
  241.     if (count < i) strcpy(temp,"");  /* no item there */
  242.     temp[j] = 0;
  243.     strcpy(outStr,temp);
  244.     return;
  245. }
  246.  
  247. /* Get the coordiates of the HyperCard card window so we can center the dialog in it. */
  248.  
  249. GetCardRect(itsRect)
  250. Rect *itsRect;
  251. {
  252.     WindowPtr cardWindow;
  253.     Rect r;
  254.     Point thePt;
  255.         
  256.     GetPort(&cardWindow);
  257.     r = (*cardWindow).portRect;
  258.     SetPt(&thePt,0,0);
  259.     LocalToGlobal(&thePt);
  260.     OffsetRect(&r,thePt.h,thePt.v);
  261.     *itsRect = r;
  262. }
  263.  
  264. /* return false if s is not a valid integer, true and number if it is */
  265.  
  266. char isNumber(s,num)
  267. char *s;
  268. long *num;
  269. {
  270.     /* s enters and exits in C format */
  271.     Str255 temp;
  272.     int i,len;
  273.     
  274.     /* see if all characters are digits */
  275.     len = strlen(s);
  276.     for (i=0;i<len;i++) {
  277.         if (!isdigit(s[i])) return FALSE;
  278.     }
  279.     CtoPstr((char *)s);
  280.     StringToNum(s,num);
  281.     PtoCstr((char *)s);
  282.     return TRUE; 
  283. }
  284.  
  285.            
  286. /* pop up a menu  */
  287.  
  288. pascal void PopList(paramPtr)
  289. XCmdBlockPtr    paramPtr;
  290. {   
  291.     int i,itsID,itemCount,realItem;
  292.     Str255 itemStr;
  293.     char topStr[32],leftStr[32],defaultStr[32];
  294.     Ptr theItems;
  295.     MenuHandle theMenu;
  296.     long theTop,theLeft,theDefault,theChoice;
  297.     Rect cardRect;
  298.     
  299.     /* check parameter count */
  300.     if (paramPtr->paramCount < 2) {
  301.         ResultIs(paramPtr,"Not enough parameters in PopUp.");
  302.         return;
  303.     }
  304.     
  305.     /* lock down the parameters so we can point to them */
  306.     
  307.     for (i=0;i<paramPtr->paramCount;i++) {
  308.         MoveHHi(paramPtr->params[i]);
  309.         HLock (paramPtr->params[i]);
  310.     }
  311.     
  312.     /* get parameters */
  313.     
  314.     theItems = *(paramPtr->params[0]);
  315.     strcpy(leftStr,*(paramPtr->params[1]));
  316.     strcpy(topStr,*(paramPtr->params[2]));
  317.     theDefault = 1;
  318.     
  319.     /* get the left,top */
  320.     
  321.     if (!isNumber(leftStr,&theLeft)) {
  322.         ResultIs(paramPtr,"Invalid Left parameter in popup");
  323.         for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  324.         return;
  325.     }
  326.     if (!isNumber(topStr,&theTop)) {
  327.         ResultIs(paramPtr,"Invalid Top parameter in popup");
  328.         for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  329.         return;
  330.     }
  331.     
  332.     
  333.     
  334.     if (paramPtr->paramCount > 3) {       /* get the default */
  335.         strcpy(defaultStr,*(paramPtr->params[3]));
  336.         if (!isNumber(defaultStr,&theDefault)) {
  337.             ResultIs(paramPtr,"Invalid default number in popup");
  338.             for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  339.             return;
  340.         }
  341.     }
  342.         
  343.     
  344.     /* get an id for this menu */
  345.     
  346.     itsID = 0;
  347.     while (itsID<128)
  348.         itsID = UniqueID('MENU');
  349.     
  350.     theMenu = NewMenu(itsID,"\pxxx");        /* create the menu */
  351.     InsertMenu(theMenu,-1);                  /* add it to the menu list */
  352.     
  353.     /* add the items */
  354.     
  355.     strcpy(itemStr,"\p ");          /* create menu with 1 blank item cuz AppendMenu */
  356.     AppendMenu(theMenu,itemStr);    /* can't handle a null string */
  357.     DelMenuItem(theMenu,1);         /* now get rid of the blank menu item */
  358.  
  359.     
  360.     /* add all items by inserting - so we aren't constrained to 255 char limit */
  361.     itemCount = NumHCItems(theItems);
  362.     realItem = 0;
  363.     for (i=0;i<itemCount;i++) {
  364.         GetHCItem(theItems,i+1,itemStr);
  365.         if (itemStr[0] == '\\')  {             /* process any meta-characters */
  366.             CtoPstr((char *)itemStr+1);
  367.             InsMenuItem(theMenu,itemStr+1,realItem+2);
  368.             realItem++;
  369.         }
  370.         else {
  371.             CtoPstr((char *)itemStr);
  372.             if (itemStr[0]!=0) {            /* ignore blank items */
  373.                 InsMenuItem(theMenu,"\p ",realItem+2); /* don't care about the name yet */
  374.                 SetItem(theMenu,realItem+1,itemStr);  /* set the item name without processing meta-characters */ 
  375.                 realItem++;
  376.             }
  377.         }
  378.     }
  379.  
  380.     SetItemMark(theMenu,theDefault,checkMark);   /* check default as per interface guidelines */
  381.     
  382.     /* pop it up */
  383.     
  384.     GetCardRect(&cardRect);
  385.     
  386.     theTop = theTop + cardRect.top;
  387.     theLeft = theLeft + cardRect.left;
  388.     theChoice = PopUpMenuSelect(theMenu,(int)theTop,(int)theLeft,(int)theDefault);
  389.     theChoice = LoWord(theChoice);
  390.     NumToString(theChoice,itemStr);
  391.     PtoCstr((char*)itemStr);
  392.     ResultIs(paramPtr,itemStr);
  393.     
  394.     DeleteMenu(itsID);
  395.     for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  396.         
  397. }
  398.  
  399. /* XCMD entry point */
  400.  
  401.  
  402. pascal void main(paramPtr)
  403. XCmdBlockPtr    paramPtr;
  404. /* this is the entry point for the XFCN */
  405. {
  406.         
  407.     RememberA0();
  408.     SetUpA4();      
  409.     PopList(paramPtr); 
  410.     RestoreA4();    
  411.  
  412.     return;
  413. }
  414.  
  415.